<template>
  <view class="top-search" :style="{paddingTop: top}">
		<!-- <view class="status-bar"> </view> -->
    <view class="location-calendar">
      <view class="location" @tap="chooseLoaction">
        <image src="/static/icon/components/location.png" class="location-img" />
        <view class="txt">{{address}}</view>
      </view>
      <!-- #ifndef MP-WEIXIN -->
        <view class="calendar">
          <image src="/static/icon/components/calendar.png" class="calendar-img"/>
          <text>{{ calendar }}</text>
        </view>
      <!-- #endif -->
    </view>
    <view class="logo-box">
      <view class="logo" @tap="previewImg(['/static/icon/components/logo.png'])"></view>
      <view class="tips"> 
        <uni-notice-bar background-color="transparent" color="#FF3B1EFF" scrollable single :text="comm?.app_index_msg" />
      </view>
      <view class="xq"> {{ day }} </view>
    </view>
    <view class="search-box">
      <uni-icons type="left" size="20" color="#333333" @tap="goBack" v-if="page == 'search' || page == 'shop'" />
      <view class="search" @tap="goSearch">
        <image src="../static/icon/components/search.png" mode="widthFix" v-if="page == 'home'" class="search-icon"/>
        <input 
          v-model="searchValue" 
          type="text" 
          :focus="isFocus" 
          :placeholder="place" 
          placeholder-style="font-size: 12px;color: #888;" 
          @focus="focus"
          class="input"
        />
        <uni-icons type="clear" class="clear" size="18" v-if="searchValue.length" @tap="clearSearch"/>
        <view class="search-btn" @tap="emit('search', searchValue)"> 搜索 </view>
      </view>
     <view class="collect" v-if="page == 'shop'">
        <view class="collect-item" @tap="collectShop">
					<view class="is-collect" :class="shopIsCollect==1?'a-is-collect':''"> </view>
					<view class="text"> 收藏 </view>
        </view>
        <view class="collect-item" @tap="complain">
					<view class="is-ts"> </view>
					<view class="text"> 投诉 </view>
        </view>
      </view>
    </view>
    <view class="shop-bg" v-if="shopImg" @click="previewImg(shopImg)">
			<swiper autoplay="true" circular="true" style="height: 100%;">
				<swiper-item v-for="item in shopImg" :key="item.url">
					<image :src="item.url" class="shop-bg-image"/>
				</swiper-item>
			</swiper>
		</view>
		<ConfirmDialog icon="locationC" title="位置授权" ref="confirmDialogRef" @confirm="openAuth"/>
  </view>
</template>

<script setup>
  import { computed, ref } from 'vue';
	import { onLoad } from '@dcloudio/uni-app'
	import ConfirmDialog from '@/components/ConfirmDialog.vue'
	import { CollectShopApi } from '@/service/index.js'
	import { currentDate, currentDay } from '@/utils/index.js'
	import { useAddressStore, useCommStore, useMiddleWare } from '@/store/index.js'
	import { previewImg } from '@/utils/index.js'
  const { statusBarHeight } = uni.getSystemInfoSync()
  const top = computed(() => statusBarHeight + 'px')
	const confirmDialogRef = ref()
  const calendar = computed(() => currentDate())
  const comm = computed(() => useCommStore().comm)
  const day = computed(() => currentDay())
  const props = defineProps({
    page: {
      type: String,
      default: 'home'
    },
		shopId: String,
		shopImg: {},
		shopIsCollect: Number,
		placeholder: {
			type: String,
			default: '您可以搜索想要的商品和信息服务'
		}
  })
	const emit = defineEmits(['search', 'isCollectChange'])
	const isFocus = ref(false)
	const searchValue = ref('')
  const place = ref('您可以搜索想要的商品和信息服务')
	const address = computed(() => {
		const { lonlat } = useAddressStore()
		return lonlat.address
	})
	const goSearch = () => {
		if(props.page == 'home') uni.navigateTo({ url: '/pages/search/search' })
	}
	const collectShop = () => {
		CollectShopApi({shop_id: props.shopId}).then(res => {
			emit('isCollectChange', res.is_collect)
		})
	}
	const goHome = () => {
		if(props.page=='home') return
		uni.switchTab({
			url: '/pages/index/index'
		})
	}
  const focus = () => {
    if(props.page == 'shop') {
      place.value = '您可以搜索想要的商品'
    } else {
      place.value = '您可以搜索想要的商品和信息服务'
    }
  }
  const clearSearch = () => {
		searchValue.value = ''
    // emit('search', '')
		// confirmDialogRef.value.open()
  }
  const goBack = () => {
    uni.navigateBack()
  }
  const complain = () => {
    uni.navigateTo({ url: `/pages/complaintShop/complaintShop?shop_id=${props.shopId}` })
  }
	const openAuth = () => {
		uni.openAppAuthorizeSetting({
			success: (res) => {
				console.log(res, 'resssssssss');
			}
		})
	}
	const chooseLoaction = () => {
		const { lonlat, SETLONLAT } = useAddressStore()
		uni.chooseLocation({
			latitude: lonlat.latitude,
			longitude: lonlat.longitude,
			success: (res) => {
				const params = {
					latitude: res.latitude,
					longitude: res.longitude,
					address: res.name
				}
				SETLONLAT(params)
			}
		})
		if(props.page=='home') return console.log('home');
		// #ifdef APP
		const appAuth = uni.getAppAuthorizeSetting()
		if (appAuth.locationAuthorized !== 'authorized') { confirmDialogRef.value.open() }
		// #endif
		uni.navigateTo({
			url: '/pages/amap/getLoactionList'
		})
	}
	onLoad(() => {
		const { SETLONLAT } = useAddressStore()
		if(props.page == 'search') isFocus.value = true
		SETLONLAT()
    if(props.page != 'home') place.value = '搜索'
	})
  defineExpose({
    searchValue
  })
</script>

<style lang="scss" scoped>
  .top-search {
    width: 100%;
    padding: 0 0 10px;
    box-sizing: border-box;
    background: url('https://mp-45b4b85b-3c09-4949-8f41-4235f6603323.cdn.bspapp.com/home-top.png') no-repeat center center;
    background-size: 100% 100%;
		.status-bar {
			height: var(--status-bar-height);
			width: 100%;
		}
    .location-calendar {
			padding: 0 12px;
      height: 44px;
      color: #333333;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .location {
				display: flex;
				align-items: center;
        font-size: 16px;
        .location-img {
          width: 20px;
					height: 20px;
          margin-right: 10px;
        }
        .txt {
          width: 176px;
          font-weight: 500;
          line-height: 19px;
          white-space:nowrap;
          text-overflow:ellipsis;
          overflow: hidden;
        }
      }
      .calendar {
        display: flex;
        align-items: center;
        width: 124px;
        height: 26px;
        border-radius: 5px;
        background-color: #FFFFFF80;
        border: 1px solid #FFFFFF;
        font-size: 12px;
        .calendar-img {
          width: 24px;
          height: 24px;
        }
      }
      }
      .logo-box {
				padding: 0 12px;
        display: flex;
        align-items: end;
        .logo {
          width: 56px;
          height: 31px;
          background: url('../static/icon/components/logo.png') no-repeat center center;
          background-size: 90% 90%;
          margin-right: 9px;
        }
        .tips {
          flex: 1;
          font-size: 14px;
          .uni-noticebar {
            margin-bottom: 0;
            padding: 0;
          }
        }
        .xq {
          display: flex;
          align-items: center;
          justify-content: center;
          width: 60px;
          height: 30px;
          margin-left: 10px;
          border-radius: 5px;
        }
    }
    .search-box {
      display: flex;
			margin-top: 6px;
			padding: 0 12px;
      align-items: center;
      .search {
        margin: 4px 0 0 6px;
        flex: 1;
        height: 32px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-size: 14px;
        padding: 0 2px 0 10px;
        border-radius: 170px;
        border: 1px solid rgba(255,59,30,0.5);
        box-sizing: border-box;
        .search-icon {
          width: 14px;
          height: 14px;
        }
        .input {
          flex: 1;
					color: #333333;
          margin: 0 10px;
					font-size: 14px;
					font-weight: 400;
        }
        .clear {
          margin-right: 8px;
        }
        .search-btn {
          color: #fff;
          width: 44px;
          height: 28px;
          font-weight: 400;
          text-align: center;
          line-height: 28px;
          background-color: #FF3B1E;
          border-radius: 139px 139px 139px 139px;
        }
      }
      .collect {
        display: flex;
				align-items: center;
        .collect-item {
          display: flex;
          flex-direction: column;
          align-items: center;
          margin-left: 11px;
          &:nth-last-of-type(1) {
            margin-left: 19px;
          }
					.is-collect {
						width: 20px;
						height: 20px;
						background: url('/static/icon/productDetails/collect.png') no-repeat center center;
						background-size: 100% 100%;
						&.a-is-collect {
							background: url('/static/icon/productDetails/a-collect.png') no-repeat center center;
							background-size: 100% 100%;
						}
					}
					.is-ts {
						width: 20px;
						height: 20px;
						background: url('/static/icon/productDetails/jubao.png') no-repeat center center;
						background-size: 100% 100%;
					}
					.text {
						color: #333333;
						font-size: 10px;
						font-weight: 400;
						line-height: 12px;
					}
        } 
      }
    }
    .shop-bg {
      width: 100%;
      height: 200px;
      margin-top: 10px;
      border-radius: 10px 10px 0 0;
			overflow: hidden;
      .shop-bg-image {
        width: 100%;
        height: 100%;
      }
    }
  }
</style>